/*!
 * tykhome - soft.js
 * Author tykhome <yongketian@gmail.com>
 */

/**
 * Module dependencies.
 */
// var Soft = require('../proxy').Soft;

var constants = require('../constants');

var Util = require('../libs/util');
var weixinUtil = require('../libs/weixin');
var weixin = weixinUtil.weixin;
var EventProxy = require('eventproxy');

/**
 * 服务器验证
 *
 * @param  {HttpRequest} req
 * @param  {HttpResponse} res
 * @param {Function} next
 */
exports.signature = function(req, res) {
	if (weixin.signature(req)) {
		res.send(200, req.query.echostr);
	} else {
		res.send(200, 'fail');
	}
};

/**
 * 接收消息
 *
 */
exports.msg = function(req, res) {
	weixin.getMsg(req, res);
}

/**
 * 创建主动消息
 *
 **/
exports.showCreateMsg = function(req, res) {
	console.log('weixin.showCreateMsg');
	res.render('weixin/createMsg');
}

/**
 * 发送消息
 *
 **/
exports.sendMsg = function(req, res) {
	console.log('weixin.sendMsg');
	var content = req.body.content;

	weixinUtil.getAccessTokenFromCache(function(err, token) {
		console.log('token: ' + token);
		if (err) return res.send(200, 'fail');
		var proxy = EventProxy.create('sendMsg', function(r) {
			var msg = JSON.parse(r);
			console.log(msg);
			res.redirect('/weixin/sendMsg');
		});

		weixin.accessToken = token;
		//发送客服文本消息
		var data = {
			'touser': 'o4hPnjhp31sjZGdxWlFc3pf4Pcb4',
			'msgtype': 'text',
			'text': {
				'content': content
			}
		};
		weixin.postKefuMsg(data, function(r) {
			proxy.emit('sendMsg', r);
		});
	});
}


/**
 * 创建主动消息
 *
 **/
exports.showUploadMediaFrom = function(req, res) {
	console.log('weixin.showUploadMediaFrom');
	weixinUtil.getAccessTokenFromCache(function(err, token) {
		if (err) return res.send(200, 'fail');
		return res.render('weixin/uploadMedia', {
			accessToken: token
		});
	});

}

/**
 * 创建用户分组
 *
 **/
exports.showcreateUserGroupFrom = function(req, res) {
	console.log('weixin.showCreateGroupFrom');
	res.render('weixin/createUserGroup');
}

/**
 * 创建用户分组
 *
 **/
exports.createUserGroup = function(req, res) {
	console.log('weixin.createUserGroup');
	var name = req.body.name;
	var data = {
		"group": {
			"name": name
		}
	};
	weixinUtil.getAccessTokenFromCache(function(err, token) {
		if (err) return res.send(200, err);
		weixinUtil.createUserGroup(token, data, function(r) {
			console.log(r);
			// res.send(200, r);
			res.redirect('/weixin/userGroup/');
		});
	});
}

/**
 * 查询用户分组
 *
 **/
exports.userGroupList = function(req, res) {
	console.log('weixin.userGroupList');
	weixinUtil.userGroupList(function(err, msg) {
		if (err) return res.send(200, err);
		var errmsg = weixinUtil.processMsg(msg);
		if (errmsg) return res.send(200, errmsg);
		res.render('weixin/userGroupList', JSON.parse(msg));
	});
}

/**
 * 查询用户所在分组
 *
 **/
exports.userGroupInfo = function(req, res) {
	console.log('weixin.userGroupInfo');
	var openid = req.params.openid;
	var data = {
		openid: openid
	}
	weixinUtil.getUserGroup(data, function(err, msg) {
		if (err) return res.send(200, err);
		// 新方法中不用手动处理errmsg了
		// var errmsg = weixinUtil.processMsg(msg);
		// if(errmsg) return res.send(200, errmsg);
		res.render('weixin/userGroup', JSON.parse(msg));
	});
}

/**
 * 创建菜单
 *
 **/
exports.menuCreate = function(req, res) {
	var data = {
		"button": [{
			"type": "click",
			"name": "今日歌曲",
			"key": "V1001_TODAY_MUSIC"
		}, {
			"type": "click",
			"name": "歌手简介",
			"key": "V1001_TODAY_SINGER"
		}, {
			"name": "菜单",
			"sub_button": [{
				"type": "view",
				"name": "搜索",
				"url": "http://www.soso.com/"
			}, {
				"type": "view",
				"name": "视频",
				"url": "http://v.qq.com/"
			}, {
				"type": "click",
				"name": "赞一下我们",
				"key": "V1001_GOOD"
			}]
		}]
	};

	weixinUtil.getAccessTokenFromCache(function(err, token) {
		if (err) return res.send(200, err); // next(err);
		var proxy = EventProxy.create('createMenu', function(msg) {
			var errmsg = weixinUtil.processMsg(msg);
			if (errmsg) return res.send(200, errmsg);
			res.send(200, msg);
		});
		// 设置accessToken
		weixin.accessToken = token;
		weixin.setCusMenu(data, function(msg) {
			proxy.emit('createMenu', msg);
		});
	});
}

exports.oauth2 = function(req, res){
	res.send(200, 'ok');
}